package controllers;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import dao.FachadaDAO;
import play.data.validation.Required;
import play.mvc.Controller;
import utils.exceptions.BDException;
import models.*;

public class DisciplinaMBean extends Controller {

	public static void index(){
		render();
	}
	
	public static void disciplina() {
    	render("Disciplina/Disciplina.html");
    }
	
	public static void disciplinaAluno() {
    	render("Disciplina/DisciplinaAluno/DisciplinaAluno.html");
    }
	
	public static void cadastrarDisciplina () {
    	render("Disciplina/cadastrarDisciplina.html");
    }
	
	public static void removerDisciplina () {
    	render("Disciplina/removerDisciplina.html");
    }
	
	public static void editarDisciplina () {
    	render("Disciplina/editarDisciplina.html");
    }
	
	public static void listarDisciplina () {
    	List<Disciplina> disciplinas = new ArrayList<Disciplina>();
		try {
			disciplinas = FachadaDAO.getInstance().listarDisciplinas();
			render("Disciplina/listarDisciplina.html",disciplinas);
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
    }
	
	public static void listarDisciplinaAluno () {
    	List<Disciplina> disciplinas = new ArrayList<Disciplina>();
		try {
			disciplinas = FachadaDAO.getInstance().listarDisciplinas();
			render("Disciplina/DisciplinaAluno/listarDisciplinaAluno.html",disciplinas);
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
    }
	
	
	public static void editarDisciplina(String codigo) {
		Disciplina disciplina;
		try {
			disciplina = FachadaDAO.getInstance().buscarDisciplina(codigo);
			render("Disciplina/editarDisciplina.html",disciplina);
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
    }
	
	public static void actionCadastrar(@Required String codigo, @Required String nome, @Required String numAulas, @Required String dataInicio, @Required String dataFim){
		
		if(validation.hasErrors()) {
			render("Disciplina/cadastrarDisciplina.html");
		}
		
		Disciplina disciplina = new Disciplina();
		disciplina.setCodigo(params.get("codigo"));
		disciplina.setNome(params.get("nome"));
		disciplina.setNumAulas(params.get("numAulas", Integer.class));
		disciplina.setDataInicio(Date.valueOf(params.get("dataInicio")));
		disciplina.setDataFim(Date.valueOf(params.get("dataFim")));
		
		try {
			FachadaDAO.getInstance().cadastrarDisciplina(disciplina);
			render("Disciplina/cadastroDisciplinaRealizado.html");
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
		
	}
	
	public static void actionEditar(@Required String codigo, @Required String nome, @Required String numAlulas, @Required String dataInicio, @Required String dataFim){
		
		if(validation.hasErrors()) {
			render("Disciplina/editarDisciplina.html");
		}
		
		Disciplina disciplina;
		
		try {
			disciplina = FachadaDAO.getInstance().buscarDisciplina(codigo);
			
			disciplina.setCodigo(params.get("codigo"));
			disciplina.setNome(params.get("nome"));
			disciplina.setNumAulas(params.get("numAulas", Integer.class));
			disciplina.setDataInicio(Date.valueOf(params.get("dataInicio")));
			disciplina.setDataFim(Date.valueOf(params.get("dataFim")));
			
			FachadaDAO.getInstance().editarDisciplina(disciplina);
			
			//listarDisciplina();
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
		
	}
	
	public static void actionRemover(String codigo) {
		try {
			FachadaDAO.getInstance().excluirDisciplina(codigo);
			listarDisciplina();
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
	}
	
	public static void actionBuscarAlterar(String codigo) {
		try {
			Disciplina disciplina = FachadaDAO.getInstance().buscarDisciplina(codigo);
			render("Disciplina/editarDisciplina.html",disciplina);
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
	}
	
	public static void actionBuscarRemover(String codigo) {
		try {
			Disciplina disciplina = FachadaDAO.getInstance().buscarDisciplina(codigo);
			render("Disciplina/removerDisciplina.html",disciplina);
		} catch (BDException e) {
			Erro erro = e.getErro();
			render("errors/erro.html",erro);
		}
	}

}
